<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Iterator的调用场合</title>
</head>
<body>
    <h1>Iterator的调用场合</h1>

    <script>
        // 1. 解构赋值
        let set = new Set(['a', 'b', 'c']);
        let [x, y] = set;
        console.log(x, y); // 'a' 'b'

        // 2. 扩展运算符
        const str = 'hello';
        console.log([...str]); // ['h', 'e', 'l', 'l', 'o']

        // 3. yield*
        function* generator() {
            yield 1;
            yield* [2, 3, 4];
            yield 5;
        }
        const gen = generator();
        for (let value of gen) {
            console.log(value); // 1, 2, 3, 4, 5
        }

        // 4. 其他场合
        // Array.from()
        const arrayFromSet = Array.from(set);
        console.log(arrayFromSet); // ['a', 'b', 'c']

        // Map, Set, WeakMap, WeakSet的构造函数
        const map = new Map([['name', '张三'], ['age', 20]]);
        for (let [key, value] of map) {
            console.log(key, value);
        }

        // Promise.all()
        const promises = Promise.all([Promise.resolve(1), Promise.resolve(2)]);
        promises.then(console.log); // [1, 2]
    </script>
</body>
</html> 